﻿var BusinessStr;
var AttributeStr;
var JNatureStr;
var ProCategoryStr;
var MarketStr;
var CountVal = 0;
var num = 0;
var strJson;
var Json;
var Arr = "";
var tal=0;

//为了让加载的数据保持正常加载,下面方法用来防止用户狂点页面
function ShowDIV(thisObjID) {
    $("#BgDiv").css({ display: "block", height: $(document).height() });
    var yscroll = document.documentElement.scrollTop;
    $("#" + thisObjID).css("top", "100px");
    $("#" + thisObjID).css("display", "block");
    
}
//关闭遮罩的效果
function closeDiv(thisObjID) {
    $("#BgDiv").css("display", "none");
    $("#" + thisObjID).css("display", "none");
}

function CheckConInfo() {
    //var Rnum = RegExp(']{0,^[+1}(\d){1,3}[   ]?([-]?((\d)|[   ]){1,12})+$');
    var Rnum = RegExp('^[0-9]{1,}|[0-9][\s]{0,}$');
    var Rmai = RegExp('^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$');
    var msgN = "请输入合法的号码";
    var msgEmail = "Please input valid format: Example@expamle.com";
    var msgNull = "";
    var status = true;

    //email 模糊查询
//    objEmail = $("#Email");
//    var EmailVals =trim(objEmail.val());
//    if (EmailVals != "") {
//        if (Rmai.test(EmailVals)) {
//            objEmail.removeAttr("style")
//            objEmail.addClass("input200");
//            objEmail.parents("#EmailInfo").find(".Warning").text(msgNull);
//        }
//        else {
//            objEmail.css("border", "1px solid red");
//            objEmail.parents("#EmailInfo").find(".Warning").text(msgEmail);
//            status = false;
//                }
//    }

    objFax = $("#Fax");
    var FaxVals = trim(objFax.val());
    if (FaxVals != "") {
        if (Rnum.test(FaxVals)) {
            objFax.removeAttr("style")
            objFax.addClass("input200");
            objFax.parents("#FaxInfo").find(".Warning").text(msgNull);
        }
        else {
            objFax.css("border", "1px solid red");
            objFax.parents("#FaxInfo").find(".Warning").text(msgN);
            status = false;
        }
    }

    objTel = $("#Tel");
    var TelVals = trim(objTel.val());
    if (TelVals != "") {
        if (Rnum.test(TelVals)) {
            objTel.removeAttr("style")
            objTel.addClass("input200");
            objTel.parents("#TelInfo").find(".Warning").text(msgNull);
        }
        else {
            objTel.css("border", "1px solid red");
            objTel.parents("#TelInfo").find(".Warning").text(msgN);
            status = false;
        }
    }
    return status;
}



function trim(str) {  //删除左右两端的空格
    return str.replace(/(^\s*)|(\s*$)/g, "");
}


$(document).ready(function () {

    //product 
    var menu1s = $("#menu1_ul1").find("li");
    menu1s.each(function (i) {
        var self = $(this);
        self.click(function () {
            //hidden first 
            selfs = self.children("span");
            var cla = selfs.attr("class");

            menu1s.children("span").removeClass();
            $("#menu_subs").find(".pro_cat1").hide();       
            
            if (cla == "") {
                selfs.addClass("span" + (i + 1))
                $("#ProCate_" + (i + 1) + "_1").show();
            }
            else {
              
                selfs.removeClass();
            }
            checkStausMn();
        });

    })

    $("#menu_subs").find("input[type='checkbox']").each(function () {
        $(this).click(function () {
            checkStausMn()
        })
    })


    //market
    var menu2s = $("#menu1_ul2").find("li")
    menu2s.each(function (i) {
        var self = $(this);
        self.click(function () {

            selfs = self.children("span");
            var cla = selfs.attr("class");

            menu2s.children("span").removeClass();
            $("#MarArea").find(".pro_cat1").hide();
           

            if (cla == "") {
                selfs.addClass("span" + (i + 1))
                $("#Area_" + (i + 1) + "_1").show();
            }
            else {
                selfs.removeClass();
            }
          
            checkStausMn2();
        });
    })
    //add even lenstion
    $("#MarArea").find("input[type='checkbox']").each(function () {
        $(this).click(function () {
            checkStausMn2()
        })
    })


});

function checkStausMn2() {
    $("#menu1_ul2").find(".selected").css("visibility", "hidden");
    var menu_subs = $("#MarArea").find(".pro_cat1");
    menu_subs.each(function (i) {
        var self = $(this).find("input[type='checkbox']"); //if have checked
        self.each(function (j) {
            if ($(this).attr("checked") == true) {
                $("#area_mn_" + (i + 1)).find(".selected").css("visibility", "visible");
                return false;
            }
        })
    })

}

function checkStausMn() {
    $("#menu1_ul1").find(".selected").css("visibility", "hidden");
    var menu_subs = $("#menu_subs").find(".pro_cat1");
    menu_subs.each(function (i) {
        var self = $(this).find("input[type='checkbox']"); //if have checked
        self.each(function (j) {
            if ($(this).attr("checked") == true) {
                $("#ProCate_" + (i + 1)).find(".selected").css("visibility", "visible");
                return false;
            }
        })
    })

}

function GetProductCategory(val, bool, Element, mark, id, title) {    
    var i = 0;
    var sum = 0;
    if (bool) {
        $.ajax({
            type: "POST",
            url: "/ServerAshxFile/GetProductCategory.ashx",
            data: { 'ProductCatUID': val },
            dataType: "json",
            timeout: 50000,
            beforeSend: function () {
                ShowDIV('DialogDiv');
            },
            complete: function (res, msg) {
                if (res.status == 0)//请求超时
                {
                }
                else if (res.status == 200)//请求成功
                {
                    var resAry = null;
                    resAry = eval(res.responseText);
                    if (resAry != null) {
                        //显示子子类
                        GetSubCategoryResponse(resAry, Element, mark, id, title);                        
                    }
                }
                else  //发生错误
                {
                    alert(title + '数据加载为成功请重新都选');
                }
            }
        });
    }
    else {
    }
}

function GetSubCategoryResponse(data, Element, mark, id, title) {
    var html = "<div class=\"sub3_title_w\"><h3 class=\"sub3_title\">" + title + "</h3><div class=\"all_btn\"><input type='checkbox' id='" + title.substring(0, 4) + "_all' name=\"" + title.substring(0, 4) + "\" onclick='GetHaveCheckedList(this.name,this.id);'/><label for=\"" + title.substring(0, 4) + "_all\" >Select All / Unclick to clear all</label></div></div><ul class=\"sub3_cat_w\">";
    var i = 0;
    $(document).ready(function () {
        $.each(data, function (entryIndex, entry) {
            if (entry['ProductSubCatEng'] != null) {
                //html += "<li><input type='checkbox' id='" + title.substring(0, 4) + "' onclick='ClearClickByAll(this)' name='ProCategory' value='" + entry['ProductSubCatEng'] + "' /><label for=\"" + title.substring(0, 4) + "\">" + entry['ProductSubCatEng'] + " " + entry['ProductSubCatChi'] + "</label></li>";
                var thid = title.substring(0, 4) + "_" + i;
                i++;
                html += "<li><input type='checkbox' id='" + title.substring(0, 4) + "' onclick='ClearClickByAll(this)' name='ProCategory' value='" + entry['ProductSubUid'] + "' /><label for=\"" + thid + "\">" + entry['ProductSubCatEng'] + " " + entry['ProductSubCatChi'] + "</label></li>";
            }
        });
    });
    html += "</ul>";
    html = "<div id='SubCat_" + mark + "_" + id + "' class=\"subcat_w\">" + html + "</div>";
    Element.append(html);  
}
function GetProductSubCat(id, val, mark, title) {
    var checkbox = $('#' + id).attr("checked");
    var DivElement = $('#SubCat_' + mark);
    if (checkbox == true) {
        $('#SubCat_' + mark + "_" + id).remove();
        GetProductCategory(val, true, DivElement, mark, id, title)
        setTimeout("closeDiv('DialogDiv')", 1000);
    }
    else {
        $('#' + id).removeAttr("checked");
        $('#' + id).parent
        $('#SubCat_' + mark + "_" + id).remove();
        GetFirstElement('#' + id);
    }    
}

function GetMarkCheckBox(id, mark_c) {
    var checkbox = $('#' + id).attr("checked");
    var GetName = $('[id=' + id.substring(0, 9) + ' ]');
    if (checkbox == true) {
        for (var i = 0; i < GetName.length; i++) {
            GetName.attr('checked', 'checked');
        }
    }
    else {
        for (var i = 0; i < GetName.length; i++) {
            GetName.removeAttr("checked");
        }
    }
}
function GetMainCheckBox(id, mark_b) {
    tal = 0,stal=0;
    var checkbox = $('#' + id).attr("checked");
    var MainDivElement = $('#' + id + '_all');
    if (checkbox == true) {
        MainDivElement.next().empty().slideDown('slow');
        MainDivElement.find('input:checkbox').each(function () {            
            $(this).attr('checked', 'checked');
            GetProductCategory(this.value, this.checked, MainDivElement.next(), mark_b, this.id, this.name)
            //当前的this.checked的值为true,在这里可以添加加载数据的对象
        });
        setTimeout("closeDiv('DialogDiv')", 3500);
    }
    else {
        MainDivElement.find('input:checkbox').each(function () {
            $(this).removeAttr("checked");
            MainDivElement.next().empty();
        });
    }
}
function GetFirstElement(Element) {
    $(Element).parent().parent().siblings().eq(1).children('input:checkbox').attr("checked", ""); ;
}

function GetHaveCheckedList(name, id) {
    var CheckAll = $("#" + id).attr('checked');
    var GetName = $('[id=' + name + ' ]');
    if (CheckAll == true) {
        for (var i = 0; i < GetName.length; i++) {
            GetName.attr('checked', 'checked');
        }
    } else {
        for (var i = 0; i < GetName.length; i++) {
            GetName.eq(i).removeAttr('checked');
        }
    }
}

function ClearClickByAll(val) {
    if (val.checked == false) {
        $("#" + val.id + "_all").removeAttr('checked');
    }
}

var strJson;
function GetInfoFromText() {
    strJson = "";
    var _customerName = trim(document.getElementById("ContentPlaceHolder2_CustomerName").value);
    if (_customerName != "") {
        strJson = "\"CustomerName\":\""+_customerName+"\",";
    }

    var _customerID =trim(document.getElementById("ContentPlaceHolder2_CustomerID").value);
    if (_customerID != "") {
        strJson = strJson + "\"CustomerID\":\""+_customerID+"\","
    }


    var _customerType = document.getElementById("ContentPlaceHolder2_CustomerType");   
    var _customerTypeVal = _customerType.options[_customerType.selectedIndex].value;
    if (_customerTypeVal != "") {
        strJson = strJson + "\"CustomerType\":\"" + trim(_customerTypeVal) + "\",";
    }

    var _membershipSince = trim(document.getElementById("ContentPlaceHolder2_MembershipSince").value);
    if (_membershipSince != "") {
        strJson = strJson + "\"MembershipSince\":\""+_membershipSince+"\",";
    }

    var _membershipStatus = document.getElementById("ContentPlaceHolder2_MembershipStatus");
    var _membershipStatusVal = _membershipStatus.options[_membershipStatus.selectedIndex].text;
    if (_membershipStatusVal != "") {
        strJson = strJson + "\"MembershipStatus\":\"" + trim(_membershipStatusVal) + "\","
    }

    var _brandName = trim(document.getElementById("ContentPlaceHolder2_BrandName").value);
    if (_brandName != "") {
        strJson = strJson + "\"BrandName\":\""+_brandName+"\",";
    }

    var _customerIntroduction = trim(document.getElementById("CustomerIntroduction").value);
    if (_customerIntroduction != "") {
        strJson = strJson + "\"CustomerIntroduction\":\""+_customerIntroduction+"\",";
    }

    var _noOfEmployee = trim(document.getElementById("NoOfEmployee").value);
    if (_noOfEmployee != "") {
        strJson = strJson + "\"NoOfEmployee\":\""+_noOfEmployee+"\",";
    }

    var _yeaEstablished = trim(document.getElementById("ContentPlaceHolder2_YeaEstablished").value);
    if (_yeaEstablished != "") {
        strJson = strJson + "\"YeaEstablished\":\""+_yeaEstablished+"\",";
    }

    var _br = trim(document.getElementById("BR").value);
    if (_br != "") {
        strJson = strJson + "\"BR\":\""+_br+"\",";
    }

    var _hkidpassportNo = trim(document.getElementById("HKIDPassportNo").value);
    if (_hkidpassportNo != "") {
        strJson = strJson + "\"HKIDPassportNo\":\""+_hkidpassportNo+"\",";
    }

    var _GC = document.getElementById("GC");
    if (_GC.checked==true) {
        strJson = strJson + "\"GC\":\""+_GC.value+"\",";
    }

    var _vip = document.getElementById("VIP");
    if (_vip.checked == true) {
        strJson = strJson + "\"VIP\":\""+_vip.value+"\",";
    }

    var _HQLocatHK = document.getElementById("HQLocatHK");
    if (_HQLocatHK.checked == true) {
        strJson = strJson + "\"HQLocatHK\":\""+_HQLocatHK.value+"\",";
    }

    var _address = trim(document.getElementById("Address").value);
    if (_address != "") {
        strJson = strJson + "\"Address\":\""+_address+"\",";
    }

    var _continent = document.getElementById("ContentPlaceHolder2_AreaInfo");
    var _continentVal = _continent.options[_continent.selectedIndex].text;
    if (_continentVal != "") {
        strJson = strJson + "\"Continent\":\"" + trim(_continentVal) + "\","
    }

    var _country  = document.getElementById("ContentPlaceHolder2_CountryInfo");
    var _countryVal = _country.options[_country.selectedIndex].text;
    if (_countryVal != "") {
        strJson = strJson + "\"Country\":\"" + trim(_countryVal)+ "\",";
    }
    
    var _position = trim(document.getElementById("Position").value);
    if (_position != "") {
        strJson = strJson + "\"Position\":\""+_position+"\",";
    }

    var _email = trim(document.getElementById("Email").value);
    if (_email != "") {
        strJson = strJson + "\"Email\":\""+_email+"\",";
    }

    var _fax = trim(document.getElementById("Fax").value);
    if (_fax != "") {
        strJson = strJson + "\"Fax\":\""+_fax+"\",";
    }

    var _tel = trim(document.getElementById("Tel").value);
    if (_tel != "") {
        strJson = strJson + "\"Tel\":\"" + _tel + "\",";
    }

    var _emailSubscribe = document.getElementById("EmailSubscribe");
    if (_emailSubscribe.checked == true) {
        strJson = strJson + "\"EmailSubscribe\":\"" + _emailSubscribe.value + "\",";       
    }

    var _faxdnc = document.getElementById("FaxDNC");
    if (_faxdnc.checked == true) {
        strJson = strJson + "\"FaxDNC\":\"" + _faxdnc.value + "\",";
    }

    var _faxunsubscribe = document.getElementById("FaxUnsubscribe");
    if (_faxunsubscribe.checked == true) {
        strJson = strJson + "\"FaxUnsubscribe\":\"" + _faxunsubscribe.value + "\",";
    }

    var _teldnc = document.getElementById("TelDNC");
    if (_teldnc.checked == true) {
        strJson = strJson + "\"TelDNC\":\"" + _teldnc.value + "\",";
    }

    var _telunsubscribe = document.getElementById("TelUnsubscribe");
    if (_telunsubscribe.checked == true) {
        strJson = strJson + "\"TelUnsubscribe\":\"" + _telunsubscribe.value + "\",";
    }

    var _contactPersonName = trim(document.getElementById("ContactPersonName").value);
    if (_contactPersonName != "") {
        strJson = strJson + "\"ContactPersonName\":\""+_contactPersonName+"\",";
    }

    var _salutation = document.getElementById("ContentPlaceHolder2_Salutation");
    var _salutationVal = _salutation.options[_salutation.selectedIndex].text;
    if (_salutationVal != "") {
        strJson = strJson + "\"Salutation\":\"" + trim(_salutationVal) + "\","
    }

    var _committeeType = document.getElementById("ContentPlaceHolder2_CommitteeType");
    var _committeeTypeVal = _committeeType.options[_committeeType.selectedIndex].text;
    if (_committeeTypeVal != "") {
        strJson = strJson + "\"CommitteeType\":\"" + trim(_committeeTypeVal) + "\","
    }


    var _commPosition = document.getElementById("ContentPlaceHolder2_CommitteePosition");
    var _commPositionVal = _commPosition.options[_commPosition.selectedIndex].text;
    if (_commPositionVal != "") {
        strJson = strJson + "\"CommitteePosition\":\"" + trim(_commPositionVal) + "\","
    }


    var _yearTerm=trim(document.getElementById("YearTerm").value);
    if (_yearTerm != "") {
        strJson = strJson + "\"YearTerm\":\""+_yearTerm+"\",";
    }

    var _productSpecifictions = trim(document.getElementById("ProductSpecifictions").value)
    if (_productSpecifictions != "") {
        strJson = strJson + "\"ProductSpecifictions\":\""+_productSpecifictions+"\",";
    }

    var _branchAddress = trim(document.getElementById("BranchAddress").value);
    if (_branchAddress != "") {
        strJson = strJson + "\"BranchAddress\":\"" + _branchAddress + "\",";
    }

    var _branchName = trim(document.getElementById("ContentPlaceHolder2_BranchName").value);
    if (_branchName != "") {
        strJson = strJson + "\"BranchName\":\"" + _branchName + "\",";
    }
    return strJson;
}

function GetAttributeSelectVal() {
    num = 0;
    Arr = "";
    var AttributeStr = "";
    $("input[name=Attribute]").each(function () {
        if (this.checked == true) {
            num++;
            if (num > 0) {
                Arr += this.value + ",";
            }
        }
    });
    if (Arr != "" & Arr != null) {
        AttributeStr = "\"AttributeStr\":\"" + Arr.substring(0, Arr.length - 1) + "\",";
    }
    else {
        AttributeStr = "";
    }
    return AttributeStr;
}

function GetJNatureSelectVal() {
    var JNatureStr = "";
    num = 0;
    Arr = "";
    $("input[name=JNature]").each(function () {
        if (this.checked == true) {
            num++;
            if (num > 0) {
                Arr += this.value + ",";
            }
        }
    });
    if (Arr != "") {
        JNatureStr = "\"JNatureStr\":\"" + Arr.substring(0, Arr.length - 1) + "\",";
    } else {
        JNatureStr = "";
    }
    return JNatureStr;
}

function GetBusinessSelectVal() {
    BusinessStr = "";
    Arr = "";
    num = 0;
    var x = document.getElementById("BNatureSelect");
    var SelectValue = x.options[x.selectedIndex].text;
    $("input[name=BNature]").each(function () {
        if (this.checked == true) {
            num++;
            if (num > 0) {
                Arr +=this.value + ",";
            }
        }
    });
    if (num >0) {
        BusinessStr = "\"BusSelVal\":\"" + SelectValue + "\",\"BusArr\":\"" + Arr.substring(0, Arr.length - 1) + "\",\"BusNum\":\"" + num + "\",";
    }
    return BusinessStr;
}

function GetProCategorySelectVal() {
    num = 0;
    Arr = "";
    ProCategoryStr = "";
    var x = document.getElementById("ProCategory");
    var SelectValue = x.options[x.selectedIndex].text;
    $("input[name=ProCategory]").each(function () {
        if (this.checked == true) {
            num++;
            if (num > 0) {
                Arr += this.value + ",";
            }
        }
    });
    if (num > 0) {
        ProCategoryStr = "\"ProSelVal\":\"" + SelectValue + "\",\"ProArr\":\"" + Arr.substring(0, Arr.length - 1) + "\",\"ProNum\":\"" + num + "\",";
    }
    return ProCategoryStr;
}

function GetMarketSelectVal() {
    num = "";
    Arr = "";
    var MarketStr = "";
    var x = document.getElementById("Market");
    var SelectValue = x.options[x.selectedIndex].text;
    $("input[name=MarArea]").each(function () {
        if (this.checked == true) {
            num++;
            if (num > 0) {
                Arr += this.value + ",";
            }
        }
    });
    if (num > 0) {
        MarketStr = "\"MarketSelVal\":\"" + SelectValue + "\",\"MarketArr\":\"" + Arr.substring(0, Arr.length - 1) + "\",\"MarketNum\":\"" + num + "\",";
    }
    return MarketStr;
}

function ReplaceStr(str) {
    var ChangStr=""
    ChangStr = str.replace("'", "''");
    return ChangStr;
}

function StringToJson(Param) {
    Json = "";
    Json = eval("({" + Param + "})");
    return Json;
}

function Save() {
    if (CheckConInfo()) {
        var JsonStr = GetInfoFromText();
        JsonStr = JsonStr + GetAttributeSelectVal();
        JsonStr = JsonStr + GetJNatureSelectVal();
        JsonStr = JsonStr + GetBusinessSelectVal();
        JsonStr = JsonStr + GetProCategorySelectVal();
        JsonStr = JsonStr + GetMarketSelectVal();
        JsonStr = JsonStr.substring(0, JsonStr.length - 1);
        //alert(JsonStr);
        JsonStr = StringToJson(JsonStr);
        $.ajax({
            type: "POST",
            url: "/ServerAshxFile/SearchResultHandle.ashx", //请求页面
            dataType: "json",
            timeout: 10000,
            data: JsonStr,
            beforeSend: function () {
            },
            complete: function (res, msg) {
                if (res.status == 0)//请求超时
                {
                }
                else if (res.status == 200)//请求成功
                {
                    window.open("SearchPageTest.aspx")

                    //location.href = "SearchPageTest.aspx"
                }
                else  //发生错误
                {
                    alert('服务器响应失败,');
                }
            }
        });
    }
}